import 'package:bike_ride/generated/l10n.dart';
import 'package:intl/intl.dart';



/// 获取车辆的品牌编号
String decodeVidBrandCode(String? s) {
  if (s == null || s == '') {
    return '';
  }
  try{
    var uri = Uri.dataFromString(s.toLowerCase());
    Map<String, String> params = uri.queryParameters;
    return params['code']!.trim();
  } catch (error) {
    return '';
  }}

///添加兼容规则的时候需要注意（很重要）
///
/// 如果是有大写的字母，一定要用小写的去判断
String decodeEquipmentCode(String? s){
  if (s == null || s == '') {
    return '';
  }

  try {
    var uri = Uri.dataFromString(s.toLowerCase());
    Map<String, String> params = uri.queryParameters;

    String result = '';
    // 查询 URL 参数
    if (params['txt'] != null && params['txt']!.isNotEmpty) {
      result = params['txt']!.trim();
    } else if (params['ewmid'] != null && params['ewmid']!.isNotEmpty) {
      result = params['ewmid']!.trim();
    } else if (params['number'] != null && params['number']!.isNotEmpty) {
      result = params['number']!.trim();
    } else if (params['id'] != null && params['id']!.isNotEmpty) {
      // 适配洪城乐行二维码，去掉V字母
      result = params['id']!.replaceAll('v', '').trim();
    } else if (params['bikesn'] != null && params['bikesn']!.isNotEmpty) {
      //适配朗朗二维码 https://m.langxu888.com/index?bikesn=910410005
      result = params['bikesn']!.trim();
    } else if (params['machineno'] != null && params['machineno']!.isNotEmpty) {
      //适配共驰出行 https://wx.gongchichuxing.com/?machineNO=07703243
      result = params['machineno']!.trim();
    } else if (params['machlineno'] != null &&
        params['machlineno']!.isNotEmpty) {
      //适配靓骑 https://yw-h5-liangqi.e-stronger.com/?machlineNO=12053
      result = params['machlineno']!.trim();
    } else if (params['carid'] != null && params['carid']!.isNotEmpty) {
      //小呗畅行:https://ebike-oper-qr.chinatowercom.cn?carId=110000491
      result= params['carid']!.trim();
    }else if(params['cno'] != null && params['cno']!.isNotEmpty){
      //彩虹出行:CarInfo.aspx?cno=10000051
      result= params['cno']!.trim();
    }else if(params['vid'] != null && params['vid']!.isNotEmpty){
      //小彬出行:https://api.xiaobinbike.cn/veh.html?vid=20210618369
      result= params['vid']!.trim();
    }else if(params['bikecode'] != null && params['bikecode']!.isNotEmpty){
      //人民出行:https://www.mmuu.com/app/down.do?bikeCode=307101056
      result= params['bikecode']!.trim();
    }else {
      // 如果字符串不符合以上所有 URL 参数
      var temp = s.split('/');

      if (temp.isNotEmpty) {
        result = temp[temp.length - 1].trim();
      } else {
        result = '';
      }
    }
    return result;
  } catch (error) {
    return '';
  }
}

/// 从 SecondSinceEpoch 时间戳格式化为时间字符串。若 [s] 为空或格式化失败，则返回空字符串。
/// 存在s ==0,转成计算机起始时间1970-01-01 08:00:00情况
/// 兼容10位、13位值例如1732604081364转换
String formatTimestamp(num? s, {String format = 'yyyy-MM-dd HH:mm:ss'}) {
  if (s == null || s == 0) {
    return '';
  }
  try {
    return DateFormat(format)
        .format(DateTime.fromMillisecondsSinceEpoch((s.toString().length<13?s * 1000:s).toInt(), isUtc: false));
  } catch (error) {
    return '';
  }
}


///时间转换 将秒转换为小时分钟 HH:mm:ss
String durationTransform(int seconds,{bool isEasy = false,bool needZeroFill = true}) {
  String hms = needZeroFill?"00:00:00":'0:0:0';
  if(isEasy) hms = needZeroFill?"00${S.current.hour}00${S.current.min}00${S.current.second}":"0${S.current.hour}0${S.current.min}0${S.current.second}";
  if (seconds > 0) {
    int h = seconds ~/ 3600;
    int m = (seconds % 3600) ~/ 60;
    int s = seconds % 60;
    hms = needZeroFill?"${zeroFill(h)}:${zeroFill(m)}:${zeroFill(s)}":'$h:$m:$s';
    if(isEasy) hms = needZeroFill?"${zeroFill(h)}${S.current.hour}${zeroFill(m)}${S.current.min}${zeroFill(s)}${S.current.second}":"$h${S.current.hour}$m${S.current.min}$s${S.current.second}";
  }

  return hms;
}

///补零
String zeroFill(int i) {
  return i >= 10 ? "$i" : "0$i";
}

/// 格式化金额
/// @param value 金额
/// @param decimal 金额小数位
/// @returns string
String formatMoney(dynamic value, [int? decimal]) {
  decimal ??= 2; // 如果decimal为null，则默认为2

  // 处理空值和无效输入
  if (value == null || (value is String && value.trim().isEmpty)) {
    return '0'.padRight(decimal + 1, '0');
  }

  // 尝试将输入转换为 double
  double parsedValue;
  if (value is num) {
    parsedValue = value.toDouble();
  } else if (value is String) {
    parsedValue = double.tryParse(value.replaceAll(',', '')) ?? 0.0;
  } else {
    return '0'.padRight(decimal + 1, '0');
  }

  // 使用 NumberFormat 进行格式化
  final formatter = NumberFormat('#,##0.${'0' * decimal}');
  return formatter.format(parsedValue);
}